package com.party.core.service.member.impl;

import com.google.common.base.Function;
import com.google.common.base.Strings;
import com.party.common.paging.Page;
import com.party.common.utils.BigDecimalUtils;
import com.party.common.utils.LangUtils;
import com.party.core.dao.read.member.CopartnerAnalyzeReadDao;
import com.party.core.dao.write.member.CopartnerAnalyzeWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.member.CopartnerAnalyze;
import com.party.core.model.member.CopartnerAnalyzeQuery;
import com.party.core.service.member.ICopartnerAnalyzeService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @Author: Administrator.
 * @Description: 合伙人数据分析
 * @Date:Created in 2017/10/26 0026 下午 16:45.
 */
@Service
public class CopartnerAnalyzeService implements ICopartnerAnalyzeService {
    @Autowired
    private CopartnerAnalyzeReadDao copartnerAnalyzeReadDao;
    @Autowired
    private CopartnerAnalyzeWriteDao copartnerAnalyzeWriteDao;

    @Override
    public String insert(CopartnerAnalyze t) {
        BaseModel.preInsert(t);
        boolean result = copartnerAnalyzeWriteDao.insert(t);
        if (result) {
            return t.getId();
        }
        return null;
    }

    @Override
    public boolean update(CopartnerAnalyze t) {
        t.setUpdateDate(new Date());
        return copartnerAnalyzeWriteDao.update(t);
    }

    @Override
    public boolean deleteLogic(String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return copartnerAnalyzeWriteDao.deleteLogic(id);
    }

    @Override
    public boolean delete(String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return copartnerAnalyzeWriteDao.delete(id);
    }

    @Override
    public boolean batchDeleteLogic(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return copartnerAnalyzeWriteDao.batchDeleteLogic(ids);
    }

    @Override
    public boolean batchDelete(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return copartnerAnalyzeWriteDao.batchDelete(ids);
    }

    @Override
    public CopartnerAnalyze get(String id) {
        return copartnerAnalyzeReadDao.get(id);
    }

    @Override
    public List<CopartnerAnalyze> listPage(CopartnerAnalyze t, Page page) {
        return copartnerAnalyzeReadDao.listPage(t, page);
    }

    @Override
    public List<CopartnerAnalyze> list(CopartnerAnalyze t) {
        return copartnerAnalyzeReadDao.listPage(t, null);
    }

    @Override
    public List<CopartnerAnalyze> batchList(Set<String> ids, CopartnerAnalyze t, Page page) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.EMPTY_LIST;
        }
        return copartnerAnalyzeReadDao.batchList(ids, new HashedMap(), page);
    }

    @Override
    public CopartnerAnalyze sum(CopartnerAnalyzeQuery query) {
        List<CopartnerAnalyze> list = this.sumList(query, null);
        if (!CollectionUtils.isEmpty(list)) {
            CopartnerAnalyze copartnerAnalyze = list.get(0);
            copartnerAnalyze.setTotalMoney(BigDecimalUtils.round(copartnerAnalyze.getTotalMoney(), 2));
            copartnerAnalyze.setTotalBrokerage(BigDecimalUtils.round(copartnerAnalyze.getTotalBrokerage(), 2));
            return copartnerAnalyze;
        }
        return new CopartnerAnalyze(0, 0, 0, 0d, 0d);
    }

    @Override
    public List<CopartnerAnalyze> sumList(CopartnerAnalyzeQuery query, Page page) {
        List<CopartnerAnalyze> list = copartnerAnalyzeReadDao.sumList(query, page);
        list = LangUtils.transform(list, new Function<CopartnerAnalyze, CopartnerAnalyze>() {
            @Override
            public CopartnerAnalyze apply(CopartnerAnalyze input) {
                if (null != input.getTotalMoney()) {
                    double round = BigDecimalUtils.round(input.getTotalMoney(), 2);
                    input.setTotalMoney(round);
                }
                if (null != input.getTotalBrokerage()) {
                    double round = BigDecimalUtils.round(input.getTotalBrokerage(), 2);
                    input.setTotalBrokerage(round);
                }
                return input;
            }
        });
        return list;
    }

    /**
     * 获取或者设置分析
     *
     * @param memberId 会员编号
     * @param date
     * @return
     */
    @Override
    public CopartnerAnalyze getSet(String memberId, Date date) {
        CopartnerAnalyze copartnerAnalyze = new CopartnerAnalyze();
        copartnerAnalyze.setMemberId(memberId);
        copartnerAnalyze.setCreateDate(date);
        List<CopartnerAnalyze> list = listPage(copartnerAnalyze, null);
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0);
        } else {
            this.insert(copartnerAnalyze);
            return copartnerAnalyze;
        }
    }

    @Override
    public CopartnerAnalyze findByMemberId(String memberId) {
        CopartnerAnalyze copartnerAnalyze = new CopartnerAnalyze();
        copartnerAnalyze.setMemberId(memberId);
        return copartnerAnalyzeReadDao.getUnique(copartnerAnalyze);
    }
}
